package com.wlyuan.open.translator;

import com.wlyuan.open.assembler.MappingOrder;
import com.wlyuan.open.order.vo.OpenOrderDetailVO;
import com.wlyuan.order.vo.OrderDetailVO;
import com.wlyuan.order.vo.OrderDraftVO;
import com.wlyuan.order.vo.OrderGoodsVO;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.Arrays;
import java.util.List;

/**
 * @author 订单转化器
 */
@Slf4j
public class OrderTranslator {
    @Mapper(uses = {
            MappingOrder.class
    }, imports = {
            Arrays.class
    })
    public interface Translator {

        @Mapping(target = "fromAddress", source = "fromAddress")
        @Mapping(target = "fromContact.name", source = "fromAddress.contactName")
        @Mapping(target = "fromContact.mobile", source = "fromAddress.contactMobile")
        @Mapping(target = "toAddress", source = "toAddress")
        @Mapping(target = "toContact.name", source = "toAddress.contactName")
        @Mapping(target = "toContact.mobile", source = "toAddress.contactMobile")
        @Mapping(target = "transport.mode", source = "transportMode")
        @Mapping(target = "transport.load", source = "load")
        @Mapping(target = "transport.unload", source = "unload")
        @Mapping(target = "transport.pick", source = "pick")
        @Mapping(target = "transport.deliver", source = "deliver")
        @Mapping(target = "transport.insure", source = "insure")
        @Mapping(target = "transport.vehicle.model", source = "vehicle.model")
        @Mapping(target = "transport.vehicle.length", source = "vehicle.length")
        @Mapping(target = "transport.vehicle.volume", source = "vehicle.volume")
        @Mapping(target = "transport.vehicle.weight", source = "vehicle.weight")
        @Mapping(target = "transport.vehicle.quantity", source = "vehicle.quantity")
        @Mapping(target = "trackingType", source = "track.trackType")
        @Mapping(target = "trackingNo", source = "track.trackNo")
        @Mapping(target = "cargoTypes", expression = "java(Arrays.asList(orderDetail.getCargoType()))")
        @Mapping(target = "shippingMode", source = "distanceType")
        @Mapping(target = "sign.type", source = "sign.signType")
//        @Mapping(target = "sign.score", source = "score.star")
//        @Mapping(target = "sign.lat",source = "score")
//        @Mapping(target = "sign.lon",source = "score")
        @Mapping(target = "sign.signTime", source = "sign.signAt")
        @Mapping(target = "items", source = "goodsList")
        @Mapping(target = "totalWeight", source = "calculateWeight")
        @Mapping(target = "totalVolume", source = "calculateVolume")
        @Mapping(target = "totalValue", source = "calculateValue")
        @Mapping(target = "transportStatus",source = "transportStatusName")
        OpenOrderDetailVO translate(OrderDetailVO orderDetail);


        @Mapping(target = "trackingType", source = "trackType")
        @Mapping(target = "trackingNo", source = "trackNo")
        @Mapping(target = "items", source = "goodsList")
        @Mapping(target = "cargoTypes", expression = "java(Arrays.asList(source.getCargoType()))")
        OpenOrderDetailVO translate(OrderDraftVO source);

        @Mapping(target = "name", source = "goodsName")
        OpenOrderDetailVO.Item translate(OrderGoodsVO spec);

        List<OpenOrderDetailVO> translate(List<OrderDetailVO> orderDetails);

    }

    private static final Translator TRANSLATOR = Mappers.getMapper(Translator.class);

    public static OpenOrderDetailVO translate(OrderDetailVO orderDetail) {
        var openDetail = TRANSLATOR.translate(orderDetail);
        if(openDetail!=null) {
            //处理经纬度
            if (orderDetail.getFromAddress() != null && StringUtils.isNotBlank(orderDetail.getFromAddress().getLocation())) {
                String[] locations = orderDetail.getFromAddress().getLocation().split(",");
                openDetail.getFromAddress().setLon(locations[0]);
                openDetail.getFromAddress().setLat(locations[1]);
            }
            if (orderDetail.getToAddress() != null && StringUtils.isNotBlank(orderDetail.getToAddress().getLocation())) {
                String[] locations = orderDetail.getToAddress().getLocation().split(",");
                openDetail.getToAddress().setLon(locations[0]);
                openDetail.getToAddress().setLat(locations[1]);
            }
            //如果打分不为空，则设置分数
            if(orderDetail.getScore()!=null && orderDetail.getScore().getStar()!=0 && orderDetail.getSign()!=null){
                openDetail.getSign().setScore(orderDetail.getScore().getStar());
            }
            // 如果回单延期原因不为空，则将签收延迟原因的值覆盖。
            if(orderDetail.getReceipt()!=null && orderDetail.getReceipt().getDelayReason()!=null){
                openDetail.getSign().setDelayReason(orderDetail.getReceipt().getDelayReason().toString());
            }
        }
        logger.info("openDetail:{}", openDetail);
        return openDetail;
    }

    public static OpenOrderDetailVO translate(OrderDraftVO source) {
        var openOrder = TRANSLATOR.translate(source);
        return openOrder;
    }

    public static List<OpenOrderDetailVO> translate(List<OrderDetailVO> orderDetails) {
        return TRANSLATOR.translate(orderDetails);
    }
}
